package com.wsm.common.listenner;

import com.wsm.common.domain.BlackipDO;
import com.wsm.common.enums.BaseEnum;
import com.wsm.common.service.BlackipService;
import com.wsm.common.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wsm.common.enums.Constant.LIMIT_KEY;

/**
 * @author wangshimin
 * 启动时加载部分所需内容
 */
@Slf4j
@Component
public class SpringbootStartListener implements CommandLineRunner {

    @Autowired
    RedisService redisService;
    @Autowired
    BlackipService blackipService;

    @Override
    public void run(String... arg0) {
        //获取黑名单
        Map<String, Object> map = new HashMap<>(2);
        map.put("delFlag" , BaseEnum.DEL_FLAF.Not_del.getCode());
        map.put("useFlag" , BaseEnum.USE_FLAG.YES.getCode());
        List<BlackipDO> list = blackipService.list(map);
        if (null != list && list.size() > 0) {
            String limitKey;
            for (BlackipDO blackipDO : list) {
                limitKey = LIMIT_KEY + blackipDO.getIp();
                if (!redisService.hasKey(limitKey)) {
                    redisService.set(limitKey, 0);
                }
            }
        }
        log.info("黑名单加载完成");
    }

}